<?php
include 'recursos.php';
$facebook = new Facebook(array(
            'appId' => '271243942927904',
            'secret' => '1cbff0dac040943a9a2c7e5257666fd2',
        ));

// Get User ID
$user = $facebook->getUser();
if ($user) {

    try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
    }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();
} else {
    $loginUrl = $facebook->getLoginUrl();
}
if ($user) {
    if (loginFacebook($user_profile)) {
        $_SESSION['idUsuario'] = obtenerIdUsuarioFacebook($user_profile);
        ?>
        accediendo...
        <META HTTP-EQUIV="REFRESH" CONTENT="0,URL=menu.php">
        <?php
    } else {
        introducirUsuarioFacebook($user_profile);
        ?>
        introduciendo
        <META HTTP-EQUIV="REFRESH" CONTENT="0,URL=index.php?mensaje=1">
        <?php
    }
}
else{
    ?>
    <META HTTP-EQUIV="REFRESH" CONTENT="0,URL=<?php echo $loginUrl?>">
     <?php
}
?>


<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
        FB.init({
            appId: '<?php echo $facebook->getAppID() ?>',
            cookie: true,
            xfbml: true,
            oauth: true
        });
        FB.Event.subscribe('auth.login', function(response) {
            window.location.reload();
        });
        FB.Event.subscribe('auth.logout', function(response) {
            window.location.reload();
        });
    };
    (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
            '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    }());
</script>
</div>